package flc.ast.fragment;

import android.Manifest;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.FileUtils;
import com.stark.picselect.PhotoSelectorBuilder;
import com.stark.picselect.config.PhotoConfig;
import com.stark.picselect.entity.SelectMediaEntity;
import com.stark.picselect.interfaces.OnSelectResultListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.CameraActivity;
import flc.ast.activity.PicEditActivity;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.StatusBarUtils;
import stark.common.basic.utils.StkPermissionHelper;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivCamera.setOnClickListener(this);
        mDataBinding.llPicBeauty.setOnClickListener(this);
        mDataBinding.llPicFilter.setOnClickListener(this);
        mDataBinding.llPicEnhance.setOnClickListener(this);
        mDataBinding.llPicPaint.setOnClickListener(this);
        mDataBinding.llPicSticker.setOnClickListener(this);
        mDataBinding.llPicColorTune.setOnClickListener(this);
        mDataBinding.llPicText.setOnClickListener(this);
        mDataBinding.llPicRotate.setOnClickListener(this);
        mDataBinding.llPicCrop.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.ivCamera) {//相机
            startActivity(CameraActivity.class);
        } else if (id == R.id.llPicBeauty) {//美颜
            getPermission(0);
        } else if (id == R.id.llPicFilter) {//滤镜
            getPermission(1);
        } else if (id == R.id.llPicEnhance) {//画质增强
            getPermission(2);
        } else if (id == R.id.llPicPaint) {//画笔
            getPermission(3);
        } else if (id == R.id.llPicSticker) {//贴纸
            getPermission(4);
        } else if (id == R.id.llPicColorTune) {//色调
            getPermission(5);
        } else if (id == R.id.llPicText) {//文字
            getPermission(6);
        } else if (id == R.id.llPicRotate) {//旋转
            getPermission(7);
        } else if (id == R.id.llPicCrop) {//裁剪
            getPermission(8);
        }
    }

    @Override
    protected void initData() {
        StatusBarUtils.setSystemStatusTextColor(true, getActivity());
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            //设置状态栏字体颜色
            StatusBarUtils.setSystemStatusTextColor(false, getActivity());
        } else {
            //设置状态栏字体颜色
            StatusBarUtils.setSystemStatusTextColor(true, getActivity());
        }
    }

    private void getPermission(int position) {
        StkPermissionHelper.permission(StkPermissionHelper.Permission.ACCESS_IMAGE_VIDEO)
                .reqPermissionDesc(getString(R.string.get_picture_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        getImage(position);
                    }
                }).request();
    }

    private void getImage(int position) {
        PhotoSelectorBuilder.builder(getActivity())
                .mode(PhotoConfig.Mode.PHOTO)
                .minCount(1)
                .maxCount(1)
                .listener(new OnSelectResultListener<List<SelectMediaEntity>>() {
                    @Override
                    public void onResult(List<SelectMediaEntity> data) {
                        ArrayList<String> pathList = new ArrayList<>();
                        for (SelectMediaEntity entity : data) {
                            pathList.add(entity.getTargetPath());
                        }
                        PicEditActivity.pos = position;
                        PicEditActivity.sImgPath = pathList.get(0);
                        startActivity(PicEditActivity.class);
                    }
                });
    }

}